package com.bytecodezz.app.photoshop.loader.strategy

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.bytecodezz.app.photoshop.loader.base.PhotoshopLoaderHandler
import com.bytecodezz.app.photoshop.loader.core.LoaderBuilder
import com.bytecodezz.app.photoshop.loader.listener.BitmapLoaderListener
import com.bytecodezz.app.photoshop.loader.listener.DrawableLoaderListener
import com.bytecodezz.app.photoshop.loader.listener.FileLoaderListener
import com.bytecodezz.app.photoshop.loader.util.Preconditions
import java.io.File

/**
 * Glide加载策略具体类
 * 使用PhotoshopLoaderManager的setHandler进行配置
 */
class Glide : PhotoshopLoaderHandler() {
    override fun loader(builder: LoaderBuilder) {
        if (Preconditions.DEBUG)
            Log.e(Preconditions.TAG, "开始glide图片加载器策略")
        //获取Glide图片加载Manager 失败则释放资源并结束
        val requestManager: RequestManager = when {
            builder.context != null -> Glide.with(builder.context!!)
            builder.fragment != null -> Glide.with(builder.fragment!!)
            builder.activity != null -> Glide.with(builder.activity!!)
            else -> {
                recycle(builder)
                return
            }
        }

        var requestBuilder = if (builder.isOnlyDownload) {
            //只进行文件下载
            requestManager.downloadOnly()
        } else {
            when {
                builder.isBitmap -> requestManager.asBitmap()
                builder.isFile -> requestManager.asFile()
                builder.isGif -> requestManager.asGif()
                else -> requestManager.asDrawable()
            }
        }

        //加载图片源
        with(requestBuilder) {
            load(
                when {
                    builder.url != null -> builder.url
                    builder.resourceId != null -> builder.resourceId
                    builder.file != null -> builder.file
                    builder.uri != null -> builder.uri
                    builder.bitmap != null -> builder.bitmap
                    builder.drawable != null -> builder.drawable
                    builder.byteArray != null -> builder.byteArray
                    else -> {
                        recycle(builder)
                        return
                    }
                }
            )
        }

        //图片加载监听
        when {
            builder.bitmapListener != null -> {
                requestBuilder = requestBuilder as RequestBuilder<Bitmap>
                val listener: BitmapLoaderListener = builder.bitmapListener!!
                requestBuilder.listener(object : RequestListener<Bitmap> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Bitmap>?,
                        isFirstResource: Boolean
                    ) = listener.onLoadFailed(e, model)

                    override fun onResourceReady(
                        resource: Bitmap?,
                        model: Any?,
                        target: Target<Bitmap>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ) = listener.onResourceReady(model, resource)
                })
            }
            builder.fileListener != null -> {
                requestBuilder = requestBuilder as RequestBuilder<File>
                val listener: FileLoaderListener = builder.fileListener!!
                requestBuilder.listener(object : RequestListener<File> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<File>?,
                        isFirstResource: Boolean
                    ) = listener.onLoadFailed(e, model)

                    override fun onResourceReady(
                        resource: File?,
                        model: Any?,
                        target: Target<File>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ) = listener.onResourceReady(model, resource)
                })
            }
            builder.drawableListener != null -> {
                requestBuilder = requestBuilder as RequestBuilder<Drawable>
                val listener: DrawableLoaderListener = builder.drawableListener!!
                requestBuilder.listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean
                    ) = listener.onLoadFailed(e, model)

                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: Target<Drawable>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ) = listener.onResourceReady(model, resource)
                })
            }
        }
        if (builder.preWidth != null && builder.preHeight != null) {
            if (builder.preHeight == 0 && builder.preWidth == 0) {
                requestBuilder.preload()
            } else {
                requestBuilder.preload(builder.preWidth!!, builder.preHeight!!)
            }
        } else {
            if (builder.isOnlyDownload || builder.imageView == null) {
                requestBuilder.preload()
            } else {
                requestBuilder.into(builder.imageView!!)
            }
        }
        if (Preconditions.DEBUG)
            Log.e(Preconditions.TAG, "结束glide图片加载器策略")
        recycle(builder)
    }
}